package com.xhwl.logistics.service.feign.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Project;
import com.xhwl.common.pojo.cent.vehicle.po.VehicleCarportRecordIntervalStatisticsPO;
import com.xhwl.common.query.cent.sdata.ProjectQuery;
import com.xhwl.common.query.cent.vehicle.VehcParkingLotQuery;
import com.xhwl.common.utils.ChkUtil;
import com.xhwl.logistics.entity.VehicleCar;
import com.xhwl.logistics.entity.VehiclePassage;
import com.xhwl.logistics.service.cent.IBIVehicleService;
import com.xhwl.logistics.service.cent.IProjectService;
import com.xhwl.logistics.service.feign.IVehicleParkingLotInfoService;
import com.xhwl.logistics.service.impl.VehicleCarServiceImpl;
import com.xhwl.logistics.service.impl.VehiclePassageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author masiyuan
 * @since 2023年05月25日 09:31
 */

//@Service
public class IVehicleParkingLotInfoServiceImpl implements IVehicleParkingLotInfoService {


    @Autowired
    private IBIVehicleService ibiVehicleService;

    @Autowired
    private IProjectService iProjectService;

    @Autowired
    private VehicleCarServiceImpl vehicleCarService;

    @Autowired
    private VehiclePassageServiceImpl vehiclePassageService;


    @Override
    public ResultJson queryVehicleParkingLotInfo(VehcParkingLotQuery query) {
        if(query.getProjectId() == null && query.getEnterpriseId() == null) {
            throw new BusinessException("项目ID和企业ID不能都为空");
        }
        List<Integer> projectIds = new ArrayList<>();
        if(ChkUtil.isNotNull(query.getProjectId())) {
            projectIds.add(query.getProjectId());
        }
        if(ChkUtil.isNotNull(query.getEnterpriseId())) {
            // 用企业ID查询出该企业下所有的项目ID
            ProjectQuery projectQuery = new ProjectQuery();
            projectQuery.setEnterpriseIds(Arrays.asList(query.getEnterpriseId()));
            List<Project> projectList = iProjectService.list(projectQuery, null);
            if(ChkUtil.isNotNull(projectList)) {
                projectIds.addAll(projectList.stream().map(Project::getId).collect(Collectors.toList()));
            }
        }
        if(ChkUtil.isNull(projectIds)) {
            return ResultJson.success();
        }
        List<VehicleCarportRecordIntervalStatisticsPO> vehicleCarportRecordIntervalStatisticsPOS = ibiVehicleService.queryCarportInfoByProj(projectIds);
        // 总车位数
        Integer totalNum = vehicleCarportRecordIntervalStatisticsPOS
                .stream().mapToInt(VehicleCarportRecordIntervalStatisticsPO::getTotalNumber).reduce(0, Integer::sum);
        // 剩余车位数
        Integer freeNum = vehicleCarportRecordIntervalStatisticsPOS
                .stream().mapToInt(VehicleCarportRecordIntervalStatisticsPO::getFreeNumber).reduce(0, Integer::sum);

        LambdaQueryWrapper<VehicleCar> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(VehicleCar::getProjectId, projectIds);
        // 固定车位数
        int vehicleCarNum = vehicleCarService.count(lambdaQueryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", totalNum);
        map.put("freeNum", freeNum);
        map.put("vehicleCarNum", vehicleCarNum);
        return ResultJson.success(map);
    }


    @Override
    public ResultJson passageStatistics(VehcParkingLotQuery query) {
        if(query.getProjectId() == null && query.getEnterpriseId() == null) {
            throw new BusinessException("项目ID和企业ID不能都为空");
        }
        List<Integer> projectIds = new ArrayList<>();
        if(ChkUtil.isNotNull(query.getProjectId())) {
            projectIds.add(query.getProjectId());
        }
        if(ChkUtil.isNotNull(query.getEnterpriseId())) {
            // 用企业ID查询出该企业下所有的项目ID
            ProjectQuery projectQuery = new ProjectQuery();
            projectQuery.setEnterpriseIds(Arrays.asList(query.getEnterpriseId()));
            List<Project> projectList = iProjectService.list(projectQuery, null);
            if(ChkUtil.isNotNull(projectList)) {
                projectIds.addAll(projectList.stream().map(Project::getId).collect(Collectors.toList()));
            }
        }
        if(ChkUtil.isNull(projectIds)) {
            return ResultJson.success();
        }

        LambdaQueryWrapper<VehiclePassage> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.in(VehiclePassage::getProjectId, projectIds);
        Integer count = vehiclePassageService.count(lambdaQueryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("count", count);
        return ResultJson.success(map);
    }
}
